package work.xiaohong.mina.test2.protocol;

import org.apache.mina.core.buffer.IoBuffer;
import work.xiaohong.mina.test2.message.GeneralMessage;
import work.xiaohong.mina.test2.message.PlcConstant;

import java.nio.charset.StandardCharsets;

/**
 * @author Kevin
 * @data 4/25/2022 1:39 PM
 * @description 编解码测试
 **/
public class ProtocolTest {

  public static void main(String[] args) {

    ProtocolDecoder protocolDecoder = new ProtocolDecoder();
    ProtocolEncoder protocolEncoder = new ProtocolEncoder();

    GeneralMessage message = new GeneralMessage();
    message.setAcknowledgementRequired(true);
    message.setPayloadType("LIFE");

    // 编码
//    try {
//      protocolEncoder.encode(null,message,null);
//    }
//    catch (Exception e) {
//      e.printStackTrace();
//    }

    // 解码
    IoBuffer buffer = IoBuffer.allocate(PlcConstant.MaxLength).setAutoExpand(true);
    // LIFE
//    String dcd = "\u00020;1;LIFE\u0003";
    // CS
//    String dcd = "\u00024;0;CONV;evt=CS;cls=[{len=0;hgt=0;her=0;car=777;wgt=0;wdt=0}];com=BCR02\u0003";
    // MO
//    String dcd = "\u0002162;0;CONV;com=BCR02;evt=MO;car=7139;dir=[999]\u0003";
    // CO
//    String dcd = "\u00023;0;CONV;evt=CO;com=BCR02;car=777;dir=1;sta=0\u0003";
    // CR
//    String dcd = "\u00027;0;CONV;evt=CR;com=GP11;car=101;sta=0\u0003";
    // RC
//    String dcd = "\u0002509;0;CONV;com=GP11;evt=RC;car=101\u0003";
    // ACK
    String dcd = "\u000235;0;ACKN;seq=765\u0003";
    buffer.put(dcd.getBytes(StandardCharsets.UTF_8));
    try {
      protocolDecoder.decode(null,buffer,null);
    }
    catch (Exception e) {
      e.printStackTrace();
    }


  }

}
